package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbUnits;
import com.ruoyi.basice_info.service.ITbUnitsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 单元设置Controller
 *
 * @author hongyi
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/basice_info/units")
public class TbUnitsController extends BaseController
{
    @Autowired
    private ITbUnitsService tbUnitsService;

    /**
     * 查询单元设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbUnits tbUnits)
    {
        startPage();
        List<TbUnits> list = tbUnitsService.selectTbUnitsList(tbUnits);
        return getDataTable(list);
    }

    /**
     * 导出单元设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:export')")
    @Log(title = "单元设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbUnits tbUnits)
    {
        List<TbUnits> list = tbUnitsService.selectTbUnitsList(tbUnits);
        ExcelUtil<TbUnits> util = new ExcelUtil<TbUnits>(TbUnits.class);
        util.exportExcel(response, list, "单元设置数据");
    }

    /**
     * 获取单元设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:query')")
    @GetMapping(value = "/{unitsId}")
    public AjaxResult getInfo(@PathVariable("unitsId") Long unitsId)
    {
        return success(tbUnitsService.selectTbUnitsByUnitsId(unitsId));
    }

    /**
     * 新增单元设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:add')")
    @Log(title = "单元设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbUnits tbUnits)
    {
        addOrEdit(tbUnits,"add");
        return toAjax(tbUnitsService.insertTbUnits(tbUnits));
    }

    /**
     * 修改单元设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:edit')")
    @Log(title = "单元设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUnits tbUnits)
    {
        addOrEdit(tbUnits,"edit");
        return toAjax(tbUnitsService.updateTbUnits(tbUnits));
    }



    public void addOrEdit(TbUnits tbUnits, String type) {
        TbUnits code = tbUnitsService.getTbUnitsByUnitsCode(tbUnits.getUnitsCode());
        TbUnits name = tbUnitsService.getTbUnitsByUnitsName(tbUnits.getUnitsName());
        if(code!=null) {
            if(type.equals("add")) {
                throw  new IllegalArgumentException("单元代码已存在");
            } else {
                if(!tbUnits.getUnitsId().equals(code.getUnitsId())) {
                    throw  new IllegalArgumentException("单元代码已存在");
                }
            }
        }
        if(name!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("单元名称已经存在");
            } else {
                if(!tbUnits.getUnitsId().equals(name.getUnitsId())) {
                    throw new IllegalArgumentException("单元名称已经存在");
                }
            }
        }
    }

    /**
     * 删除单元设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:units:remove')")
    @Log(title = "单元设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitsIds}")
    public AjaxResult remove(@PathVariable Long[] unitsIds)
    {
        return toAjax(tbUnitsService.deleteTbUnitsByUnitsIds(unitsIds));
    }
}
